package fem2.jlinalg;

import inf.jlinalg.IMatrix;
import inf.jlinalg.SolveFailedException;
import fem2.enu.EchoLevelType;
import fem2.jnative.JMKL;

public class MKLLSESolver extends Solver {

	static {
		if (!JMKL.isAvailable()) {
			throw new UnsatisfiedLinkError("JMKL is not available");
		}
	}

	private IMatrix A;

	public MKLLSESolver() {
	}

	@Override
	public IMatrix getA() {
		return A;
	}

	@Override
	public void setSize(int dimk) {
	}

	@Override
	public void setSize(SymbolicTable t) {
		A = t.createCSCMatrix();
	}

	@Override
	public void initialize() {
		((CSCMatrix) A).initialize();
	}

	@Override
	public void solve(double[] b) throws SolveFailedException {
		long start = System.currentTimeMillis();
		CSCMatrix A = (CSCMatrix) getA();
		int n = A.getColumnCount();
		double[] x = new double[n];
		int nz = A.getNonzeros();
		JMKL.solve(n, nz, A.columnPointer(), A.rowIndex(), A.values(), x, b);
		System.arraycopy(x, 0, b, 0, n);
		long end = System.currentTimeMillis();
		if (getEchoLevel().contain(EchoLevelType.AUX1)) {
			System.out.println("Solving time: " + (end - start) + " ms.");
		}
	}

	@Override
	public void finalize() {
	}

}
